package com.itheima.upload.controller;

import com.itheima.entity.vo.Result;
import com.itheima.upload.oss.OssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2022-12-22 17:35
 * description :
 */
@RestController
@RequestMapping("/upload")
public class MsgUploadController {

    @Autowired
    private OssProperties ossProperties;

    @PostMapping()
    public Result upload(MultipartFile file) throws IOException {
        //取出来的格式为im ages/png
        String contentType = file.getContentType();
        if (contentType==null){
            throw new RuntimeException("文件类型错误");
        }
        String[] split = contentType.split("/");
        String type = split[1];

        List<String> match = new ArrayList<>();
        match.add("png");
        match.add("jpg");
        match.add("bmp");
        if (!match.contains(type)){
            throw new RuntimeException("文件格式错误");
        }

        InputStream inputStream = file.getInputStream();
        String upload = ossProperties.upload(type,inputStream);
        return Result.success(upload);
    }
}
